<?php
namespace Library;

/**
 * 用户信息处理类
 *
 * @author pufan
 */
class User {
    private static $_instance;
    protected $user_model;

    private function __construct() {
        $this->user_model= D("RealUsers");
    }

    public function __clone() {
        trigger_error('Clone is not allow!', E_USER_ERROR);
    }

    public static function getInstance(){
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    function addPhone($phone) {
        $openid = $this->getCurrentOpenId();
        if (empty($openid)) {
            return FALSE;
        }

        try {
            $data['phone'] = $phone;
            $this->user_model->where(array('weixin_id'=>$_SESSION['open_id']))->save($data);
        } catch (\Exception $exc) {
            \Think\Log::record('addPhone:' . $exc->getTraceAsString());
            return FALSE;
        }
        return TRUE;
    }

    /*
     * 检测当前用户信息是否完整
     */
    function checkUserInfo() {
        $userinfo = $this->getCurrentUserInfo();

        if ($userinfo && is_array($userinfo) && $userinfo['realname'] != '' && $userinfo['phone'] != ''
            && $userinfo['address'] != '' ) {
            return TRUE;
        }
        return FALSE;
    }

    /*
     * 保存用户信息
     */
    function saveCurrentUserInfo($data) {
        $where['weixin_id'] = $this->getCurrentOpenId();
        $this->user_model->where($where)->save($data);
        \Think\Log::record('saveCurrentUserInfo: userid:'.$where['weixin_id'].json_encode($data));
    }

    /*
     * 获取当前用户信息
     */
    function getCurrentUserInfo() {
        $where['weixin_id'] = $this->getCurrentOpenId();
        $userinfo = $this->user_model->where($where)->find();

        return $userinfo;
    }

    function getCurrentOpenId() {
        return $_SESSION['open_id'];
    }

    function getUserId() {
        if($_SESSION['user_id']) {
            return $_SESSION['user_id'];
        } else if ($this->getCurrentOpenId()) {
            $userid = $this->user_model->where(array('weixin_id'=>$this->getCurrentOpenId()))->getField('id');
            if (!$userid) {
                \Think\Log::record('getUserId sql:'. $this->user_model->getLastSql());
            }
            return $userid;
        }
    }

    function saveWxUserInfo($userInfo) {
        $wxuser_info = $userInfo;

        $user_id = D('RealUsers')->where(array('weixin_id'=>$_SESSION['open_id']))->getField('id');

        if ($user_id) {
            $data['status'] = 1;
            $data['last_login'] = date("Y-m-d H:i:s",time());
            $data['last_login_ip'] = get_client_ip(0,true);
            if ($wxuser_info) {
            	$data['weixin_avatar'] = $wxuser_info['avatar'];
            }
            D('RealUsers')->where(array('weixin_id'=>$_SESSION['open_id']))->save($data);
        } else {
            $data['register_from'] = 1;
            if ($wxuser_info) {
            	$data['status'] = 1;
                $data['weixin_id'] = $wxuser_info['open_id'];
                $data['nickname'] = $wxuser_info['nickname'];
                $data['weixin_avatar'] = $wxuser_info['avatar'];
                $data['gender'] = $wxuser_info['sex'];
                $data['address'] = $wxuser_info['location'];
                $data['created_at'] = date("Y-m-d H:i:s",time());
            	$data['last_login'] = date("Y-m-d H:i:s",time());
            	$data['last_login_ip'] = get_client_ip(0,true);
                $result = D('RealUsers')->add($data);
                if ($result){
                    $user_id = D('RealUsers')->getLastInsID();
                    $user['real_user_id'] = $user_id;
                    D('user_wallet')->add($user);
                }
            }
        }
        if ($user_id) {
           return $user_id;
        }

        return true;
    }
    function getUserLocation() {
        $where['openid'] = $this->getCurrentOpenId();
        $location = D('user_location')->field('longitude, latitude')->where($where)->order('add_time desc')->find();
        return $location;
    }
    function createUserbyMobile($phone) {
        $data['phone'] = $phone;
        $data['register_from'] = 4;
        $data['weixin_id'] = $_SESSION['open_id'];
        $userid = D('RealUsers')->add($data);
        return $userid;
    }
    function getUseridbymobile($phone) {
        $user_id = 0;
        $user_id = D('RealUsers')->where(array('phone'=>$phone))->getField('id');
        return $user_id;
    }

}
